package com.szbber.commen.base

import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.launcher.ARouter
import com.szbber.commen.view.SwipeBackActivity.SwipeBackActivity
import com.szbber.medical.commen.R

//持有俩个泛型，VM和DB，分别是BaseViewModel和ViewDataBinding的实现类
abstract class BaseVMActivity<V : ViewDataBinding, VM : BaseViewModel> : SwipeBackActivity() {

    //是否打印生命周期
    var openLifecycle: Boolean = false
    protected lateinit var mBinding: V
    protected lateinit var mViewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        //取消状态栏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        val params: WindowManager.LayoutParams = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, bindLayout())
        mBinding.lifecycleOwner = this
        mViewModel = initVM()
        initView()

    }

    @LayoutRes
    abstract fun bindLayout(): Int

    abstract fun initView()

    abstract fun initVM(): VM


    private fun finishActivity() {
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out)
    }

    fun startActivityIn(
        intent: Intent?,
        curAct: Activity
    ) {
        if (intent != null) {
            curAct.startActivity(intent)
            curAct.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out)
        }
    }

    override fun finish() {
        super.finish()
        finishActivity()
    }


    //Arouter路由跳转封装， action代表Postcard方法体回调
    fun open(path: String, requestCode: Int = 0, action: Postcard.() -> Unit = {}) {
        val postcard = ARouter.getInstance().build(path)
        postcard.action()
        postcard.navigation(this, requestCode)
    }

    fun openWithFinish(path: String, action: Postcard.() -> Unit = {}) {
        open(path, 0, action)
        finish()
    }

    override fun onResume() {
        super.onResume()
//        LogUtil.e(TAG, "onResume", openLifecycle)
    }

    override fun onPause() {
        super.onPause()
//        LogUtil.e(TAG, "onPause", openLifecycle)
    }

    override fun onStart() {
        super.onStart()
//        LogUtil.e(TAG, "onStart", openLifecycle)
    }

    override fun onRestart() {
        super.onRestart()
//        LogUtil.e(TAG, "onRestart", openLifecycle)
    }

    override fun onStop() {
        super.onStop()
//        LogUtil.e(TAG, "onStop", openLifecycle)
    }

    override fun onDestroy() {
        super.onDestroy()
//        LogUtil.e(TAG, "onDestroy", openLifecycle)
    }


}